#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：Employment_class 
@File    ：11_tftp_uploading.py
@IDE     ：PyCharm 
@Author  ：XiaoDengYa
@Date    ：2021/6/23/023 15:24 
"""

from socket import *
import struct
import sys
#
# ip = ""
# filename = ""
# s = None
#
#
# def handle_argv():
#     """处理输入参数"""
#     global ip
#     global filename
#
#     if len(sys.argv) != 3:
#         print("-" * 30)
#         print("请输入正确格式：python xxx.py ip filename")
#         print("例如：python xxx.py 127.0.0.1, test.txt")
#         print("-" * 30)
#     else:
#         ip = sys.argv[1]
#         filename = sys.argv[2]
#
#
# def upload_file():
#     """上传文件"""
#     code = 0
#     block_number = 0
#     current_number = 1
#
#     while True:
#         data, addr = s.recvfrom(1024)
#
#         code, block_number = struct.unpack("!HH", data[:4])
#
#         if code == 4:
#             if block_number == 0:
#                 f = open(filename, 'rb')
#
#             if block_number + 1 == current_number:
#                 file_data = f.read(512)
#                 send_data = struct.pack('!HH', 3, current_number) + file_data
#                 s.sendto(send_data, addr)
#                 print("第{}次传输数据！".format(current_number))
#
#                 block_number += 1
#                 current_number += 1
#
#             if len(send_data) < 516:
#                 f.close()
#                 print("文件上传完成！")
#                 break
#
#         elif code == 5:
#             f.close()
#             print("上传文件失败！error info: {}".format(data[4:].decode()))
#             break
#
#
# def main():
#     global s
#
#     handle_argv()
#
#     upload_args = struct.pack("!H{}sb5sb".format(len(filename)), 2, filename.encode(), 0, "octet".encode(), 0)
#
#     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#
#
#     s.sendto(upload_args, ("192.168.10.125", 69))
#
#     upload_file()
#
#     s.close()
#
#
# if __name__ == "__main__":
#     main()



def main():
    uploadingFileName = input("请输入你需要上传的文件名：")
    udpSocket = socket(AF_INET,SOCK_DGRAM)
    upload_args =  struct.pack("!H{}sb5sb".format(len(uploadingFileName)), 2, uploadingFileName.encode("gb2312"), 0, "octet".encode("gb2312"), 0)
    udpSocket.sendto(upload_args,("192.168.10.125",69))
    current_number = 1
    block_number = 0
    while True:
        data,addr = udpSocket.recvfrom(1024)

        code,block_number = struct.unpack("!HH",data[:4])
        print("data:%s"%data)
        print("addr:{}".format(addr))
        print("code:{}".format(code))
        print("block_number:{}".format(block_number))
        if code == 4:
            if block_number == 0:
                f = open(uploadingFileName,"rb")

            if block_number +1 == current_number:
                file_data = f.read(512)
                send_data = struct.pack("!HH",3,current_number) +file_data
                udpSocket.sendto(send_data,addr)
                print("第{}次传输数据！".format(current_number))
                block_number += 1
                current_number += 1

            if len(send_data) < 516:
                f.close()
                print("文件上传完成！")
                break
        elif code == 5:
            f.close()
            print("文件上传失败error info :{}".format(data[4:].decode()))
            break
    udpSocket.close()

if __name__ == '__main__':
    main()



